package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbApproveConfig;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbApproveConfigDao {
    @Results(id = "pcbApproveConfigResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "flow_type", property = "flowType", jdbcType = JdbcType.INTEGER),
            @Result(column = "flow_type_name", property = "flowTypeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "approve_node", property = "approveNode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_factory_id", property = "parentFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "parent_factory_name", property = "parentFactoryName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "son_factory_id", property = "sonFactoryId", jdbcType = JdbcType.INTEGER),
            @Result(column = "son_factory_name", property = "sonFactoryName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "operator", property = "operator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "operator2", property = "operator2", jdbcType = JdbcType.VARCHAR),
            @Result(column = "operator3", property = "operator3", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })
    /*
     * 单个查询
     */
    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.APPROVE_CONFIG +
            "<where>" +
            "<if test='id != null'>" +
            " and id = #{id}" +
            "</if>" +
            "</where>" +
            " order by sort" +
            "</script>")
    PcbApproveConfig getById(@Param("id") Integer id);

    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.APPROVE_CONFIG +
            "<where>" +
            "<if test='flowType != null'>" +
            " and flow_type = #{flowType}" +
            "</if>" +
            "<if test='approveNode != null and approveNode != \"\"'>" +
            " and approve_node = #{approveNode}" +
            "</if>" +
            "<if test='sonFactoryId != null'>" +
            " and son_factory_id = #{sonFactoryId}" +
            "</if>" +
            "</where>" +
            " order by sort" +
            "</script>")
    PcbApproveConfig getOneByLimit(@Param("flowType") Integer flowType,
                                   @Param("approveNode") String approveNode,
                                   @Param("sonFactoryId") Integer sonFactoryId);

    @Select("<script> " +
            "select * " +
            "from " + DB2TableName.APPROVE_CONFIG +
            "<where>" +
            "<if test='flowType != null'>" +
            " and flow_type = #{flowType}" +
            "</if>" +
            "<if test='flowTypeName != null and flowTypeName != \"\"'>" +
            " and flow_type_name like CONCAT('%',#{flowTypeName},'%')" +
            "</if>" +
            "<if test='parentFactoryName != null and parentFactoryName != \"\"'>" +
            " and parent_factory_name like CONCAT('%',#{parentFactoryName},'%')" +
            "</if>" +
            "<if test='sonFactoryName != null and sonFactoryName != \"\"'>" +
            " and son_factory_name like CONCAT('%',#{sonFactoryName},'%')" +
            "</if>" +
            "<if test='approveNode != null and approveNode != \"\"'>" +
            " and approve_node = #{approveNode}" +
            "</if>" +
            "<if test='sonFactoryId != null'>" +
            " and son_factory_id = #{sonFactoryId}" +
            "</if>" +
            "</where>" +
            " order by sort" +
            "</script>")
    @ResultMap(value = "pcbApproveConfigResultMap")
    List<PcbApproveConfig> getAllByLimit(PcbApproveConfig param);

    @Insert("insert into " + DB2TableName.APPROVE_CONFIG + " ( flow_type, flow_type_name, approve_node, parent_factory_id,parent_factory_name, son_factory_id,son_factory_name, operator, operator2, operator3, remark, create_time, create_user, update_time, update_user, status, sort) values " +
            " (#{flowType}, #{flowTypeName}, #{approveNode}, #{parentFactoryId},#{parentFactoryName},#{sonFactoryId},#{sonFactoryName}, #{operator}, #{operator2}, #{operator3}, #{remark}, #{createTime}, #{createUser}, #{updateTime}, #{updateUser}, #{status}, #{sort})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insert(PcbApproveConfig param);

    /*
     *更新
     */
    @Update("<script>" +
            "update " + DB2TableName.APPROVE_CONFIG +
            "<set>" +
            "<if test='flowType != null'>" +
            "flow_type = #{flowType}," +
            "</if>" +
            "<if test='flowTypeName != null'>" +
            "flow_type_name = #{flowTypeName}," +
            "</if>" +
            "<if test='approveNode != null and approveNode != \"\"'>" +
            "approve_node = #{approveNode}," +
            "</if>" +
            "<if test='parentFactoryId != null'>" +
            "parent_factory_id = #{parentFactoryId}," +
            "</if>" +
            "<if test='parentFactoryName != null'>" +
            "parent_factory_name = #{parentFactoryName}," +
            "</if>" +
            "<if test='sonFactoryId != null'>" +
            "son_factory_id = #{sonFactoryId}," +
            "</if>" +
            "<if test='sonFactoryName != null'>" +
            "son_factory_name = #{sonFactoryName}," +
            "</if>" +
            "<if test='operator != null'>" +
            "operator = #{operator}," +
            "</if>" +
            "<if test='operator2 != null'>" +
            "operator2 = #{operator2}," +
            "</if>" +
            "<if test='operator3 != null'>" +
            "operator3 = #{operator3}," +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "remark = #{remark}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "update_time = #{updateTime}," +
            "</if>" +
            "<if test='updateUser != null'>" +
            "update_user = #{updateUser}," +
            "</if>" +
            "<if test='sort != null'>" +
            "sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "status = #{status}," +
            "</if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    Integer update(PcbApproveConfig param);

    /*
     *通过id删除
     */
    @Delete("delete from " + DB2TableName.APPROVE_CONFIG + " where id = #{id} ")
    Integer deleteById(@Param("id") Integer id);

}
